# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import xml.dom.minidom as xml

from AdventureMoney.core import ShareProfile

class Save:
	
	VERSION_STRING = "2"
	
	def __init__(self, file, account):
		doc = xml.Document()
		self.SaveAccountToXML(doc, doc, account)
		XMLstring = doc.toprettyxml()
		
		file.write(XMLstring)
	
	def SaveAccountToXML(self, doc, parentNode, account):
		accountNode = doc.createElement("Account")
		parentNode.appendChild(accountNode)
		accountNode.setAttribute("version", self.VERSION_STRING)
		
		peopleNode = doc.createElement("People")
		accountNode.appendChild(peopleNode)
		for person in sorted(account.people):
			self.SavePersonToXML(doc, peopleNode, person, account)
			
		categoryNode = doc.createElement("Categories")
		accountNode.appendChild(categoryNode)
		for category in sorted(account.categories):
			self.SaveCategoryToXML(doc, categoryNode, category)
			
		billNode = doc.createElement("Bills")
		accountNode.appendChild(billNode)
		for bill in account.bills:
			self.SaveBillToXML(doc, billNode, bill)
	
	def SavePersonToXML(self, doc, parentNode, person, account):
		node = doc.createElement("Person")
		parentNode.appendChild(node)
		
		node.setAttribute("name", str(person))
		if account.isManager(person):
			node.setAttribute("manager", "True")
	
	def SaveCategoryToXML(self, doc, parentNode, category):
		node = doc.createElement("Category")
		parentNode.appendChild(node)
		
		node.setAttribute("name", str(category))
		node.setAttribute("paidBy", str(category.getPaidBy() or ""))
		
		shareProfile = category.getSharedBy()
		for person in shareProfile:
			shareType, value = shareProfile.getPersonShare(person)
			self.SaveShareToXML(doc, node, person, shareType, value)
		
	def SaveBillToXML(self, doc, parentNode, bill):
		billNode = doc.createElement("Bill")
		parentNode.appendChild(billNode)
		
		billNode.setAttribute("value", str( bill.getValue() ))
		billNode.setAttribute("date", str( bill.getDate() ))
		
		if bill.getDetails():
			billNode.setAttribute("details", str( bill.getDetails() ))
		if bill.getComments():
			billNode.setAttribute("comments", str( bill.getComments() ))
		if bill.paidBy:
			billNode.setAttribute("paidBy", str(bill.paidBy))
		
		if bill.category:
			billNode.setAttribute("category", str(bill.category))
		else:
			shareProfile = bill.getSharedBy()
			for person in shareProfile:
				shareType, value = shareProfile.getPersonShare(person)
				self.SaveShareToXML(doc, billNode, person, shareType, value)
			
	def SaveShareToXML(self, doc, parentNode, person, shareType, value):
		if shareType == ShareProfile.SHARE_NONE:
			return
		
		node = doc.createElement("Share")
		parentNode.appendChild(node)
		
		node.setAttribute("person", str(person))
		if shareType == ShareProfile.SHARE_EQUAL:
			node.setAttribute("type", "equal")
		elif shareType == ShareProfile.SHARE_EXACT:
			node.setAttribute("type", "exact")
			node.setAttribute("value", str(value))
		elif shareType == ShareProfile.SHARE_PERCENT:
			node.setAttribute("type", "percent")
			node.setAttribute("value", str(value))

